---
title: hexo next 美化
date: 2021-09-20 17:20:15 
tags:
---
本文介绍了在 Next 主题的基础上进一步对博客进行美化的方案，主要包括:

- 在文章末尾添加结束标记
- 修改侧边栏的位置到左边
- 添加不同类型的动态背景效果
- 添加 live2d 看板娘
- 为布局元素添加边缘弹性摆动效果
- 个性化回到顶部按钮
- 添加不同类型的鼠标点击特效
- 评论区输入打字礼花特效

读者可以根据需要选择其中喜欢的方案应用到站点博客中。



# 修改博客字体

在 [Google Fonts](https://www.google.com/fonts) 上找到心仪的字体，然后在主题配置文件中为不同的应用场景配置字体：

```
themes\next\_config.yml
font:
  enable: true

  # 外链字体库地址，例如 //fonts.googleapis.com (默认值)
  host:

  # 全局字体，应用在 body 元素上
  global:
    external: true
    family: Monda

  # 标题字体 (h1, h2, h3, h4, h5, h6)
  headings:
    external: true
    family: Roboto Slab

  # 文章字体
  posts:
    external: true
    family:

  # Logo 字体
  logo:
    external: true
    family:

  # 代码字体，应用于 code 以及代码块
  codes:
    external: true
    family:
```

# 文章页末美化

## 为标签添加图标

默认情况下标签前缀是 `#` 字符，用户可以通过修改主题源码将标签的字符前缀改为图标前缀，更改后效果如下：

[![标签图标前缀](http://yearito-1256884783.image.myqcloud.com/hexo-theme-beautify/20181113045027074.png)](http://yearito-1256884783.image.myqcloud.com/hexo-theme-beautify/20181113045027074.png)

[标签图标前缀](http://yearito-1256884783.image.myqcloud.com/hexo-theme-beautify/20181113045027074.png)



在文章布局模板中找到文末标签相关代码段，将 `#` 换成 `<i class="fa fa-tags"></i>` 即可：

```
themes\next\layout\_macro\post.swig
  <footer class="post-footer">
    {% if post.tags and post.tags.length and not is_index %}
      <div class="post-tags">
        {% for tag in post.tags %}
-          <a href="{{ url_for(tag.path) }}" rel="tag"># {{ tag.name }}</a>
+          <a href="{{ url_for(tag.path) }}" rel="tag"><i class="fa fa-tags"></i> {{ tag.name }}</a>
        {% endfor %}
      </div>
    {% endif %}
    ...
  </footer>
```

Next 中使用 [FontAwesome](https://fontawesome.com/v4.7.0/icons/) 作为图标库，用户可以在 FontAwesome 上找到心仪的图标来替换标签的字符前缀。

## 添加结束标记

本章节参考 [asdfv1929 | Hexo NexT 主题内给每篇文章后添加结束标语](https://asdfv1929.github.io/2018/01/28/add-the-end/)

在文末添加结束标记，效果如下：

[![文末结束标记](http://yearito-1256884783.image.myqcloud.com/hexo-theme-beautify/20181113045252399.png)](http://yearito-1256884783.image.myqcloud.com/hexo-theme-beautify/20181113045252399.png)

[文末结束标记](http://yearito-1256884783.image.myqcloud.com/hexo-theme-beautify/20181113045252399.png)



新建布局模板文件 post-end-tag.swig，添加如下代码：

```
themes\next\layout\_macro\post-end-tag.swig
<div>
  {% if not is_index %}
    <div style="text-align:center;color:#bfbfbf;font-size:16px;">
      <span>-------- 本文结束 </span>
      <i class="fa fa-{{ config.post_end_tag.icon }}"></i>
      <span> 感谢阅读 --------</span>
    </div>
  {% endif %}
</div>
```

在文章布局模板中添加如下代码：

```
themes\next\layout\_macro\post
{#####################}
{### END POST BODY ###}
{#####################}

+ {% if config.post_end_tag.enabled and not is_index %}
+   <div>
+     {% include 'post-end-tag.swig' %}
+   </div>
+ {% endif %}

{% if theme.wechat_subscriber.enabled and not is_index %}
  <div>
    {% include 'wechat-subscriber.swig' %}
  </div>
{% endif %}
```

在站点配置文件末尾添加如下代码：

```
_config.yml
post_end_tag:
  enabled: true  # 是否开启文末的本文结束标记
  icon: paw # 结束标记之间的图标
```

重启服务器后即可在文末看到结束标记。

# 页面加载进度条

当网络不好的时候可能会在打开站点或跳转文章时出现短暂的白屏，此时如果能有加载进度提示将会提高用户操作体验。

在根目录下执行以下命令安装相关依赖：

```
$ git clone https://github.com/theme-next/theme-next-pace themes/next/source/lib/pace
```

在主题配置文件中设置 `pace: true`。

默认提供了多种主题的进度条加载样式，有顶部提示的，有中间提示的，还有全页面遮挡提示的，个人认为默认的进度条效果就恰如其当，既能够在页面空白的时候起到加载作用，也不会因为太过花里胡哨而喧宾夺主，尤其是当你如果使用了不蒜子的站点访问统计的功能的时候，常常会遇到所有资源都加载完毕而不蒜子还在等待响应，如果这个时候在页面较显眼的位置出现一个停滞不前的进度条，很让人抓狂。

# 侧边栏放左边

受 [猪猪侠的博客](https://www.ofind.cn/) 所启发，萌生了想把主题侧边栏放在左侧的想法。

Next 主题各系列中只有 Pisces 和 Gemini 支持通过主题配置文件来将侧边栏置于左侧或右侧，而 Muse 和 Mist 则需要深度修改源码才能实现改变侧边栏位置。

在自定义样式文件中添加如下规则：

```
themes\next\source\css\_custom\custom.styl
.sidebar-toggle {
  left: 30px;
}

.sidebar {
  left: 0px;
}
```

修改动效脚本代码：

```
themes\next\source\js\src\motion.js
$(document)
  .on('sidebar.isShowing', function() {
    NexT.utils.isDesktop() && $('body').velocity('stop').velocity(
-     {paddingRight: SIDEBAR_WIDTH},
+     {paddingLeft: SIDEBAR_WIDTH},
      SIDEBAR_DISPLAY_DURATION
    );
  })
  .on('sidebar.isHiding', function() {
  });
  ...
  hideSidebar: function() {
-   NexT.utils.isDesktop() && $('body').velocity('stop').velocity({paddingRight: 0});
+   NexT.utils.isDesktop() && $('body').velocity('stop').velocity({paddingLeft: 0});
    this.sidebarEl.find('.motion-element').velocity('stop').css('display', 'none');
    this.sidebarEl.velocity('stop').velocity({width: 0}, {display: 'none'});

    sidebarToggleLines.init();
    ...
}
```

如此以来就可以将侧边栏放置在左边了，但当窗口宽度缩小到 991px 之后会出现样式错误：侧边栏收缩消失但是页面左侧仍留有空白间距，此时修改如下代码即可：

```
themes\next\source\css\_common\scaffolding\base.stylbody {  position: relative; // Required by scrollspy  font-family: $font-family-base;  font-size: $font-size-base;  line-height: $line-height-base;  color: $text-color;  background: $body-bg-color;- +mobile() { padding-left: 0 !important; }- +tablet() { padding-left: 0 !important; }  + +mobile() { padding-right: 0 !important; }+ +tablet() { padding-right: 0 !important; }  +desktop-large() { font-size: $font-size-large; }}
```

# 添加动态背景

Next 主题可以通过安装插件快速为站点添加不同效果的动态背景。

## 粒子漂浮聚合

应用效果如下图：

[![canvas-nest 动态背景](http://yearito-1256884783.image.myqcloud.com/hexo-theme-beautify/20181115092103046.png)](http://yearito-1256884783.image.myqcloud.com/hexo-theme-beautify/20181115092103046.png)

[canvas-nest 动态背景](http://yearito-1256884783.image.myqcloud.com/hexo-theme-beautify/20181115092103046.png)



该功能由 [theme-next-canvas-nest](https://github.com/theme-next/theme-next-canvas-nest) 插件提供，在根目录下执行如下命令：

```
$ git clone https://github.com/theme-next/theme-next-canvas-nest themes/next/source/lib/canvas-nest
```

然后在主题配置文件中设置 `canvas_nest: true` 即可。

Next v6.5.0 及以上版本支持更多的自定义选项：

```
themes\next\_config.ymlcanvas_nest:  enable: true  onmobile: true # 是否在移动端显示  color: '0,0,255' # 动态背景中线条的 RGB 颜色  opacity: 0.5 # 动态背景中线条透明度  zIndex: -1 # 动态背景的 z-index 属性值  count: 99 # 动态背景中线条数量
```

## Three 三维动效

[theme-next-three](https://github.com/theme-next/theme-next-three) 插件提供了三个类型的背景动效，应用效果如下：

- [three-waves](http://yearito.cn/posts/hexo-theme-beautify.html#three-三维动效-1)
- [canvas-lines](http://yearito.cn/posts/hexo-theme-beautify.html#three-三维动效-2)
- [canvas-sphere](http://yearito.cn/posts/hexo-theme-beautify.html#three-三维动效-3)

  [![img](http://yearito-1256884783.image.myqcloud.com/hexo-theme-beautify/20181115091950761.png)](http://yearito-1256884783.image.myqcloud.com/hexo-theme-beautify/20181115091950761.png)

在根目录下执行如下命令安装相关依赖：

```
$ git clone https://github.com/theme-next/theme-next-three themes/next/source/lib/three
```

然后在主题配置文件中设置开启对应的动效选项即可。

```
themes\next\_config.yml# JavaScript 3D library.# Dependencies: https://github.com/theme-next/theme-next-three# three_wavesthree_waves: true# canvas_linescanvas_lines: false# canvas_spherecanvas_sphere: false
```

个人认为在站点中添加动态背景并没有实际的意义，只会凭空增加页面内存占用及 CPU 消耗，所以本站没有添加任何动态背景。

## 随机三角丝带

[![随机三角丝带](http://yearito-1256884783.image.myqcloud.com/hexo-theme-beautify/evan-you.png)](http://yearito-1256884783.image.myqcloud.com/hexo-theme-beautify/evan-you.png)

[随机三角丝带](http://yearito-1256884783.image.myqcloud.com/hexo-theme-beautify/evan-you.png)



该功能由 Vue 作者 [尤雨溪](http://evanyou.me/) 首创。本章节中核心代码来源于 [DIYgod](https://diygod.me/) 编写的 [sagiri](https://github.com/DIYgod/hexo-theme-sagiri) 主题。

点击下方按钮下载相应的脚本，并置于 themes\next\source\js\ 目录下：

[随机三角丝带](https://script-1256884783.file.myqcloud.com/evan-you.js)

在主题自定义布局文件中添加以下代码：

```
themes\next\layout\_custom\custom.swig{# 随机三角丝带背景 #}{% if theme.evanyou %}  <canvas id="evanyou"></canvas>  <style>    #evanyou {      position: fixed;      width: 100%;      height: 100%;      top: 0;      left: 0;      z-index: -1;    }  </style>  <script src="/js/evan-you.js"></script>{% endif %}
```

如果 custom.swig 文件不存在，需要手动新建并在布局页面中 body 末尾引入：

```
themes\next\layout\_layout.swig      ...      {% include '_third-party/exturl.swig' %}      {% include '_third-party/bookmark.swig' %}      {% include '_third-party/copy-code.swig' %}+     {% include '_custom/custom.swig' %}    </body>  </html>
```

在主题配置文件中添加以下代码：

```
themes\next\_config.yml# colorful trilateral riband backgroundevanyou: true
```

如果从本地加载 JS 脚本速度较慢，可以考虑将脚本放到 CDN 上再引入。

# 添加看板娘

本章节部分内容参考 [FJKang | 添加一个萌物](https://fjkang.github.io/2017/12/08/添加一个萌物/)

该功能由 [hexo-helper-live2d](https://github.com/EYHN/hexo-helper-live2d) 插件支持，效果如下图：

[![live2d 看板娘](http://yearito-1256884783.image.myqcloud.com/hexo-theme-beautify/live2d.gif)](http://yearito-1256884783.image.myqcloud.com/hexo-theme-beautify/live2d.gif)

[live2d 看板娘](http://yearito-1256884783.image.myqcloud.com/hexo-theme-beautify/live2d.gif)



在站点根目录下执行以下命令安装依赖：

```
$ npm install --save hexo-helper-live2d
```

在站点配置文件中添加以下下配置项

```
_config.yml# Live2D# https://github.com/EYHN/hexo-helper-live2dlive2d:  enable: true  pluginRootPath: live2dw/  pluginJsPath: lib/  pluginModelPath: assets/ Relative)  # 脚本加载源  scriptFrom: local # 默认从本地加载脚本  # scriptFrom: jsdelivr # 从 jsdelivr CDN 加载脚本  # scriptFrom: unpkg # 从 unpkg CDN 加载脚本  # scriptFrom: https://cdn.jsdelivr.net/npm/live2d-widget@3.x/lib/L2Dwidget.min.js # 从自定义地址加载脚本  tagMode: false # 只在有 {{ live2d() }} 标签的页面上加载 / 在所有页面上加载  log: false # 是否在控制台打印日志  # 选择看板娘模型  model:    use: live2d-widget-model-shizuku  # npm package的名字    # use: wanko # /live2d_models/ 目录下的模型文件夹名称    # use: ./wives/wanko # 站点根目录下的模型文件夹名称    # use: https://cdn.jsdelivr.net/npm/live2d-widget-model-wanko@1.0.5/assets/wanko.model.json # 自定义网络数据源  display:    position: left # 显示在左边还是右边    width: 100 # 宽度    height: 180 # 高度  mobile:    show: false  react:    opacityDefault: 0.7 # 默认透明度
```

更多配置参数请查看 [L2Dwidget | live2d-widget.js](https://l2dwidget.js.org/docs/class/src/index.js~L2Dwidget.html#instance-method-init)

此时重启服务器暂时还看不到看板娘，需要手动下载或安装模型资源。可以从 [hexo live2d 模型预览](https://huaji8.top/post/live2d-plugin-2.0/) 里找到你喜欢的角色，然后根据 [live2d-widget-models](https://github.com/xiazeyu/live2d-widget-models) 中提供的方法来下载模型数据.

例如通过以下命令下载模型 shizuku：

```
$ npm install live2d-widget-model-shizuku
```

因为修改了站点配置文件，所以需要重启服务器才能预览模型效果。

如果设置了 `live2d.tagMode: true`，则可以在指定页面中插入以下标签：

```
{{ live2d() }}
```

只有拥有该标签的页面才会渲染 live2d 模型，这样以来就可以精确控制在哪些页面上显示看板娘了。

如果只想在一级菜单页面上显示看板娘，可以在 Header 模板中添加以下代码：

```
themes\next\layout\_partials\header\index.swig+ {% if is_index %}+   {{ live2d() }}+ {% endif %}
```

个人认为在文章内出现看板娘将会影响读者注意力的集中，毕竟一篇博客里最重要的是内容，而不是这些花里胡哨转移注意力的东西。所以本站只在一级菜单页面添加了看板娘，文章页面则保持极致精简的阅读体验。

经过测试发现 `live2d.mobile.show: false` 并没有生效，暂时没有找到好的解决方法，参考 [EYHN/hexo-helper-live2d Issues #12](https://github.com/EYHN/hexo-helper-live2d/issues/12) 后发现可以在自定义样式文件中添加以下代码来解决：

```
themes/next/source/css/_custom/custom.styl#live2dcanvas {  +mobile() {    display: none;  }  +tablet() {    display: none;  }}
```

不要乱点不该点的地方，会生气的。

# 边缘摆动效果

[![wobblewindow 边缘摆动](http://yearito-1256884783.image.myqcloud.com/hexo-theme-beautify/wobblewindow.gif)](http://yearito-1256884783.image.myqcloud.com/hexo-theme-beautify/wobblewindow.gif)

[wobblewindow 边缘摆动](http://yearito-1256884783.image.myqcloud.com/hexo-theme-beautify/wobblewindow.gif)



在 [猪猪侠的博客](https://www.ofind.cn/) 里发现的这种特效，觉得挺有意思的，就从他 Github 上给扒过来了

点击下方按钮下载脚本，并置于 themes\next\source\js\ 目录下：

[wobblewindow.js](https://script-1256884783.file.myqcloud.com/wobblewindow.js)

在主题自定义布局文件中添加以下代码：

```
themes\next\layout\_custom\custom.swig{# wobble窗口摆动特效 #}{% if theme.wobble %}  <script src="/js/wobblewindow.js"></script>  <script>    //只在桌面版网页启用特效    if( window.innerWidth > 768  ){      $(document).ready(function () {        {% if theme.wobble.header %}          $('#header').wobbleWindow({            radius: {{ theme.wobble.radius }},            movementTop: false,            movementLeft: false,            movementRight: false,            debug: false,          });        {% endif %}        {% if theme.wobble.sidebar %}          $('#sidebar').wobbleWindow({            radius: {{ theme.wobble.radius }},            movementLeft: false,            movementTop: false,            movementBottom: false,            position: 'fixed',            debug: false,          });        {% endif %}        {% if theme.wobble.footer %}          $('#footer').wobbleWindow({            radius: {{ theme.wobble.radius }},            movementBottom: false,            movementLeft: false,            movementRight: false,            offsetX: {{ theme.wobble.offset }},            position: 'absolute',            debug: false,          });        {% endif %}      });    }  </script>{% endif %}
```

如果 custom.swig 文件不存在，需要手动新建并在布局页面中 body 末尾引入：

```
themes\next\layout\_layout.swig      ...      {% include '_third-party/exturl.swig' %}      {% include '_third-party/bookmark.swig' %}      {% include '_third-party/copy-code.swig' %}+     {% include '_custom/custom.swig' %}    </body>  </html>
```

在自定义样式文件中添加以下样式：

```
themes\next\source\css\_custom\custom.styl//窗口波动效果相关样式if hexo-config('wobble')  {  .sidebar {    box-shadow: none;  }  .wobbleTransparentBK{    background-color: rgba(0,0,0,0) !important;  }  .wobbleTransparentLine{    border-color: rgba(0,0,0,0) !important;  }  //Next.Muse中为Header和Footer添加背景色  #header, #footer {    background-color: rgb(245, 245, 245);  }  //防止sidebar和footer同时开启动效时堆叠异常  #sidebar, header {    z-index: 1 !important;  }  //防止挡住页末文章的阅读全文按钮  .main {    padding-bottom: 200px;  }}
```

Next.Muse 主题方案中 Header 和 Footer 是没有背景色的，所以需要添加背景色后才能看出边缘摆动效果。另外，实现边缘摆动效果所需的 `z-index` 属性可能会导致元素堆叠异常，需要添加以上样式来矫正。

在主题配置文件中添加以下代码：

```
themes\next\_config.yml# window wobllewobble:  enable: true  # 是否开启边缘波动效果  radius: 50  # 波动半径  sidebar: true  # 开启侧边栏边缘摆动  header: true  # 开启头部边缘摆动  footer: true  # 开启脚部边缘摆动
```

用户可以根据需要在配置文件中为选择开启边缘摆动效果的布局元素。刷新浏览器，然后将鼠标移动到布局边缘上尽情的挑逗它吧。

如果从本地加载 JS 脚本速度较慢，可以考虑将脚本放到 CDN 上再引入。

# 个性化回到顶部

从 [DIYgod 的博客](https://diygod.me/) 里扒来的，效果如下：

[![回到顶部](http://yearito-1256884783.image.myqcloud.com/hexo-theme-beautify/back-to-top.gif)](http://yearito-1256884783.image.myqcloud.com/hexo-theme-beautify/back-to-top.gif)

[回到顶部](http://yearito-1256884783.image.myqcloud.com/hexo-theme-beautify/back-to-top.gif)



原理很简单，将 back-to-top 按钮添加图片背景，并添加 CSS3 动效即可。

首先，找到自己喜欢的图片素材放到 source\images\ 目录下。

你可以点击下方按钮下载本站所使用的小猫上吊素材（ 小猫咪这么可爱，当然要多放点孜然啦…）

[      下载图片 ](http://yearito.cn/images/scroll.png)

然后在自定义样式文件中添加如下代码：

```
themes\next\source\css\_custom\custom.styl//自定义回到顶部样式.back-to-top {  right: 60px;  width: 70px;  //图片素材宽度  height: 900px;  //图片素材高度  top: -900px;  bottom: unset;  transition: all .5s ease-in-out;  background: url("/images/scroll.png");  //隐藏箭头图标  > i {    display: none;  }  &.back-to-top-on {    bottom: unset;    top: 100vh < (900px + 200px) ? calc( 100vh - 900px - 200px ) : 0px;  }}
```

刷新浏览器即可预览效果。

# 鼠标点击特效

从各个站点里搜罗了以下四个比较常用的鼠标点击特效：

- [礼花特效](http://yearito.cn/posts/hexo-theme-beautify.html#鼠标点击特效-1)
- [爆炸特效](http://yearito.cn/posts/hexo-theme-beautify.html#鼠标点击特效-2)
- [浮出爱心](http://yearito.cn/posts/hexo-theme-beautify.html#鼠标点击特效-3)
- [浮出文字](http://yearito.cn/posts/hexo-theme-beautify.html#鼠标点击特效-4)

  [![img](http://yearito-1256884783.image.myqcloud.com/hexo-theme-beautify/cursor-fireworks.gif)](http://yearito-1256884783.image.myqcloud.com/hexo-theme-beautify/cursor-fireworks.gif)

点击下方按钮下载相应的脚本，并置于 themes\next\source\js\cursor\ 目录下：

[礼花特效](https://script-1256884783.file.myqcloud.com/cursor/fireworks.js) [爆炸特效](https://script-1256884783.file.myqcloud.com/cursor/explosion.min.js) [浮出爱心](https://script-1256884783.file.myqcloud.com/cursor/love.min.js) [浮出文字](https://script-1256884783.file.myqcloud.com/cursor/text.js)

在主题自定义布局文件中添加以下代码：

```
themes\next\layout\_custom\custom.swig{# 鼠标点击特效 #}{% if theme.cursor_effect == "fireworks" %}  <script async src="/js/cursor/fireworks.js"></script>{% elseif theme.cursor_effect == "explosion" %}  <canvas class="fireworks" style="position: fixed;left: 0;top: 0;z-index: 1; pointer-events: none;" ></canvas>  <script src="//cdn.bootcss.com/animejs/2.2.0/anime.min.js"></script>  <script async src="/js/cursor/explosion.min.js"></script>{% elseif theme.cursor_effect == "love" %}  <script async src="/js/cursor/love.min.js"></script>{% elseif theme.cursor_effect == "text" %}  <script async src="/js/cursor/text.js"></script>{% endif %}
```

如果 custom.swig 文件不存在，需要手动新建并在布局页面中 body 末尾引入：

```
themes\next\layout\_layout.swig      ...      {% include '_third-party/exturl.swig' %}      {% include '_third-party/bookmark.swig' %}      {% include '_third-party/copy-code.swig' %}+     {% include '_custom/custom.swig' %}    </body>  </html>
```

在主题配置文件中添加以下代码：

```
themes\next\_config.yml# mouse click effect: fireworks | explosion | love | textcursor_effect: fireworks
```

这样即可在配置文件中一键快速切换鼠标点击特效。

如果从本地加载 JS 脚本速度较慢，可以考虑将脚本放到 CDN 上再引入。

# 打字特效

本章节参考 [千灵夙赋 | Hexo 优化汇总 #31](https://qianling.pw/hexo-optimization/)，原文出自 [龙笑天下 | 给 WordPress 博客网站添加评论输入打字礼花及震动特效](https://www.ilxtx.com/comment-input-effects.html)

[![打字特效](http://yearito-1256884783.image.myqcloud.com/hexo-theme-beautify/typing-effect.gif)](http://yearito-1256884783.image.myqcloud.com/hexo-theme-beautify/typing-effect.gif)

[打字特效](http://yearito-1256884783.image.myqcloud.com/hexo-theme-beautify/typing-effect.gif)



点击下方按钮下载相应的脚本，并置于 themes\next\source\js\ 目录下：

[打字特效](https://script-1256884783.file.myqcloud.com/activate-power-mode.min.js)

在主题自定义布局文件中添加以下代码：

```
themes\next\layout\_custom\custom.swig
{# 打字特效 #}
{% if theme.typing_effect %}
  <script src="/js/activate-power-mode.min.js"></script>
  <script>
    POWERMODE.colorful = {{ theme.typing_effect.colorful }};
    POWERMODE.shake = {{ theme.typing_effect.shake }};
    document.body.addEventListener('input', POWERMODE);
  </script>
{% endif %}
```

如果 custom.swig 文件不存在，需要手动新建并在布局页面中 body 末尾引入：

```
themes\next\layout\_layout.swig
      ...
      {% include '_third-party/exturl.swig' %}
      {% include '_third-party/bookmark.swig' %}
      {% include '_third-party/copy-code.swig' %}

+     {% include '_custom/custom.swig' %}
    </body>
  </html>
```

在主题配置文件中添加以下代码：

```
themes\next\_config.yml
# typing effect
typing_effect:
  colorful: true  # 礼花特效
  shake: false  # 震动特效
```

如果从本地加载 JS 脚本速度较慢，可以考虑将脚本放到 CDN 上再引入。


# 文章页面个性化设置

## 添加评论功能

Next 支持多款评论系统：

- [Disqus](https://disqus.com/)：欧美 UI 风格，支持 Tweet、Facebook 等国外社交软件的三方登陆和一键分享。 [Demo](https://blog.disqus.com/disqus-welcomes-the-spruce)
- [gitment](https://github.com/imsun/gitment)：必须用 github 账号登陆才能评论，支持 Markdown 语法，与 github issues 页面风格一致 [Demo](https://imsun.github.io/gitment/)
- [Valine](https://valine.js.org/)：支持匿名评论，支持 Markdown 语法，界面简洁美观
- [畅言](http://changyan.kuaizhan.com/)：国产评论系统，可区分热评和最新评论，论坛贴吧风
- [来必力](https://www.livere.com/)：支持插入图片和 GIF，支持国内外多种社交媒体的三方登陆 [Demo](https://www.livere.com/city-demo)

博客的评论系统不需要太过复杂的功能，我的要求是一定要轻量级，足够简洁美观，并且支持 Markdown 语法，因此我首选 Valine 和 gitment，这两个评论系统都是由国内个人开发的，在此向开发者致敬。

[![Valine 评论系统](http://yearito-1256884783.image.myqcloud.com/hexo-advanced-settings/valine-comments.png)](http://yearito-1256884783.image.myqcloud.com/hexo-advanced-settings/valine-comments.png)

[Valine 评论系统](http://yearito-1256884783.image.myqcloud.com/hexo-advanced-settings/valine-comments.png)



Next 已经内置了 Valine 组件，在主题配置文件中开启评论功能即可，同时，由于 Valine 是基于 Leancloud 提供后端服务的，所以需要填写 LeanCloud 的 App ID 和 App Key。

```
themes\next_config.yml
valine:
  enable: true
  appid:  ***<app_id***
  appkey: ***<app_key>***
  notify: false  # 收到新评论是否邮件通知
  verify: false  # 是否开启验证码
  placeholder:  # 默认填充文字
  avatar: mm  # 设置默认评论列表
  guest_info: nick,mail  # 评论区头部表单
  pageSize: 10  # 每页评论数
  visitor: true  # 同时开启文章阅读次数统计
```

Valine 也附带了阅读统计功能，可以在 Valine 配置项中设置 `visitor: true` 开启该功能。为避免后端服务冲突，建议不要同时启用 Valine 的阅读统计功能和 `leancloud_visitors`。

关于如何在收到评论时发送邮件提醒请参照 [赵俊 | Hexo 优化 — Valine 扩展之邮件通知](http://www.zhaojun.im/hexo-valine-admin/)

Next 暂时不支持通过配置的方式隐藏文章标题下的评论数量，如要隐藏，可在自定义样式文件中添加如下代码：

```
themes/next/source/css/_custom/custom.styl
//屏蔽标题下的评论数量
.post-comments-count {
  display: none;
}
```

如果你是轻度洁癖患者，想要隐藏评论区的浏览器和操作系统版本号以拥有更加干净的评论界面，可在自定义样式文件中添加如下代码：

```
themes/next/source/css/_custom/custom.styl
//屏蔽评论组件的多余信息
#comments .vsys {
  display: none;
}
```

## 文末版权声明

[![文章版权声明](http://yearito-1256884783.image.myqcloud.com/hexo-advanced-settings/post-copyright.png)](http://yearito-1256884783.image.myqcloud.com/hexo-advanced-settings/post-copyright.png)

[文章版权声明](http://yearito-1256884783.image.myqcloud.com/hexo-advanced-settings/post-copyright.png)



在主题配置文件中开启文章底部的版权声明，版权声明默认使用 [CC BY-NC-SA 4.0](https://creativecommons.org/licenses/by-nc-sa/4.0/) 许可协议，用户可以根据自身需要修改 `licence` 字段变更协议。

```
themes\next_config.yml
post_copyright:
  enable: true
  license: <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" rel="external nofollow" target="_blank">CC BY-NC-SA 4.0</a>
```

默认版权声明中只有 **本文作者**、**本文链接**、**版权声明** 三项，如果你想添加更多内容，如 **创建时间**、**修改时间**、**引用链接** 等，需要修改版权声明的相关代码：

```
themes\next\layout_macro\post-copyright.swig
<!-- JS库 clipboard 拷贝内容到粘贴板-->
<script src="https://cdn.bootcss.com/clipboard.js/2.0.1/clipboard.min.js"></script>

<!-- JS库 sweetalert 显示提示信息-->
<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>

<ul class="post-copyright">
  <!-- 本文标题 -->
  <li>
    <strong>{{ __('post.copyright.title') + __('symbol.colon') }} </strong>
    {{ post.title }}
  </li>

  <!-- 本文作者 -->
  <li class="post-copyright-author">
    <strong>{{ __('post.copyright.author') + __('symbol.colon') }} </strong>
    {{ post.author | default(author) }}
  </li>

  <!-- 创建时间 -->
  <li>
    <strong>{{ __('post.created') + __('symbol.colon') }} </strong>
    {{ post.date.format("YYYY年MM月DD日 - HH时MM分") }}
  </li>

  <!-- 修改时间 -->
  <li>
    <strong>{{ __('post.modified') + __('symbol.colon') }} </strong>
    {{ post.updated.format("YYYY年MM月DD日 - HH时MM分") }}
  </li>

  <!-- 引用链接 -->
  <li class="post-copyright-link">
    <strong>{{ __('post.copyright.link') + __('symbol.colon') }}</strong>
    <a href="{{ post.url | default(post.permalink) }}" title="{{ post.title }}"
      >{{ post.url | default(post.permalink) }}</a
    >
    <span class="copy-path" title="点击复制引用链接"
      ><i
        style="cursor: pointer"
        class="fa fa-clipboard"
        data-clipboard-text="[{{ post.author | default(author) }}'s Blog | {{ post.title }}]({{ post.permalink }})"
        aria-label="{{ __('post.copy_success') }}"
      ></i
    ></span>
  </li>

  <!-- 版权声明 -->
  <li class="post-copyright-license">
    <strong
      >{{ __('post.copyright.license_title') + __('symbol.colon') }}
    </strong>
    {{ __('post.copyright.license_content', theme.post_copyright.license) }}
  </li>
</ul>

<script>
  var clipboard = new ClipboardJS(".fa-clipboard");
  clipboard.on("success", function(target) {
    var message = document.createElement("div");
    message.innerHTML =
      '<i class="fa fa-check-circle message-icon"></i><span class="message-content">' +
      target.trigger.getAttribute("aria-label") +
      "</span>";
    swal({
      content: message,
      className: "copy-success-message",
      timer: 1000,
      button: false
    });
  });
</script>
```

在版权样式文件中添加如下样式：

```
themes\next\source\css_common\components\post\post-copyright.styl
.swal-overlay {
  background-color: transparent;
}

.copy-success-message {
  box-shadow: 0px 4px 12px rgba(0, 0, 0, 0.15);
  border-radius: 4px;
  width: auto;
  margin: 16x 0px;
  vertical-align: top;
}

.copy-success-message .swal-content {
  margin: 0px 0px !important;
  padding: 10px 16px;
  line-height: 1em;
}

.copy-success-message .message-icon {
  color: #52c41a;
  margin-right: 8px;
}

.copy-success-message .message-content {
  font-size: 14px;
}
```

然后补全版权信息文案字段：

```
themes/next/languages/zh-CN.yml
post:
  created: 创建时间
  modified: 修改时间
  copy_success: 复制成功
  copyright:
    title: 本文标题
    author: 本文作者
    link: 引用链接
    license_title: 版权声明
    license_content: "本博客所有文章除特别声明外，均采用 %s 许可协议。转载请注明出处！"
```

点击引用链接后的图标  即可快捷复制 Markdown 引用链接，并将弹出复制成功的提示语。该功能主要借助 [clipboard](https://clipboardjs.com/) 和 [sweetalert](https://sweetalert.js.org/) 两个 js 库来实现，并参考 [antDesign | message 组件](https://ant.design/components/message/) 重写了弹框样式。

在实际使用过程中，并非每篇文章都需要版权声明，如果转载了别人的文章，文末再出现个人版权声明就不太合适。此时可在 Front-Matter 中设定变量 `copyright` 用于控制是否显示版权信息。修改文章布局模板中相关代码，使得只有当主题配置文件中 `post_copyright.enable` 字段和 `page.copyright` 字段同时为 `true` 时才会插入版权声明：

```
themes/next/layout/_macro/post.swig
- {% if theme.post_copyright.enable and not is_index %}
+ {% if theme.post_copyright.enable and page.copyright and not is_index %}
    <div>
      {% include 'post-copyright.swig' with { post: post } %}
    </div>
  {% endif %}
```

为了批量为每篇新文章设定该变量并赋默认值，可以修改草稿模板内容，这样以来每篇草稿发布为正文后都会默认显示底部版权信息：

```
scaffolds\draft.md  title: {{ title }}  tags:  categories:+ copyright: true
```

## 添加打赏功能

[![文章打赏功能](http://yearito-1256884783.image.myqcloud.com/hexo-advanced-settings/post-reward.png)](http://yearito-1256884783.image.myqcloud.com/hexo-advanced-settings/post-reward.png)

[文章打赏功能](http://yearito-1256884783.image.myqcloud.com/hexo-advanced-settings/post-reward.png)



启用主题配置文件中的打赏相关字段，并将个人收款码图片置于 themes\next\source\images\ 目录下，注意保持图片命名与配置文件中一致：

```
themes\next_config.ymlreward_comment:wechatpay: /images/wechatpay.pngalipay: /images/alipay.jpg
```

如果要关闭悬停收款码上的文字抖动效果，可以在自定义样式文件中添加以下代码：

```
themes/next/source/css/_custom/custom.styl//关闭打赏收款码的文字抖动效果#QR > div:hover p {  animation: none;}
```

并非每个页面都需要开启打赏功能，可以在 Front-Matter 中添加 `reward` 字段来控制是否在本文章中添加打赏信息，然后修改文章布局模板中相关的判定条件：

```
themes/next/layout/_macro/post.swig- {% if (theme.alipay or theme.wechatpay or theme.bitcoin) and not is_index %}+ {% if ( post.reward and (theme.alipay or theme.wechatpay or theme.bitcoin) and not is_index %}    <div>      {% include 'reward.swig' %}    </div>  {% endif %}
```

为了方便可在草稿模板 scaffolds\draft.md 中统一添加 `reward` 字段默认值：

```
scaffolds\draft.md  title: {{ title }}  tags:  categories:+ reward: true
```

## 添加图片灯箱

添加灯箱功能，实现点击图片后放大聚焦图片，并支持幻灯片播放、全屏播放、缩略图、快速分享到社交媒体等，该功能由 [fancyBox](https://github.com/fancyapps/fancybox) 提供，效果如下：

[![fancyBox 灯箱](http://yearito-1256884783.image.myqcloud.com/hexo-advanced-settings/fancybox.png)](http://yearito-1256884783.image.myqcloud.com/hexo-advanced-settings/fancybox.png)

[fancyBox 灯箱](http://yearito-1256884783.image.myqcloud.com/hexo-advanced-settings/fancybox.png)



在根目录下执行以下命令安装相关依赖：

```
$ git clone https://github.com/theme-next/theme-next-fancybox3 themes/next/source/lib/fancybox
```

在主题配置文件中设置 `fancybox: true`：

```
themes\next_config.ymlfancybox: true
```

刷新浏览器即可生效。

## 相关文章推荐

该功能由 [hexo-related-popular-posts](https://github.com/tea3/hexo-related-popular-posts) 插件提供，效果如下：

[![相关文章推荐](http://yearito-1256884783.image.myqcloud.com/hexo-advanced-settings/related-post.png)](http://yearito-1256884783.image.myqcloud.com/hexo-advanced-settings/related-post.png)

[相关文章推荐](http://yearito-1256884783.image.myqcloud.com/hexo-advanced-settings/related-post.png)



在站点根目录中执行以下命令安装依赖：

```
$ npm install hexo-related-popular-posts --save
```

在主题配置文件中开启相关文章推荐功能：

```
themes\next_config.ymlrelated_posts:  enable: true  title: # custom header, leave empty to use the default one  display_in_home: false  params:    maxCount: 5
```

此时会在每篇文章结尾根据标签相关性和内容相关性来推荐相关文章。

事实上并非每篇文章都需要开启该功能，可在文章 Front-Matter 中设置 `related_posts` 字段来控制是否在文末显示相关文章，然后修改文章布局模板中相关的判定条件：

```
themes/next/layout/_macro/post.swig
- {% if theme.related_posts.enable and (theme.related_posts.display_in_home or not is_index) %}
+ {% if theme.related_posts.enable and (theme.related_posts.display_in_home or not is_index) and post.related_posts %}
    {% include 'post-related.swig' with { post: post } %}
  {% endif %}
```

为了方便可在草稿模板 scaffolds\draft.md 中统一添加 `related_posts` 字段默认值：

```
scaffolds\draft.md
  title: {{ title }}
  tags:
  categories:
+ related_posts: true
```

## 文章评分

[widgetpack](https://widgetpack.com) 是一款轻量级的插件，提供四项具体的功能：

- Comments: 评论系统，类似于留言板
-  Reviews: 评价系统，类似于商品评价
-  Rating: 星级评分系统
-  Google Reviews: 关联展示 Google Rating

Next 主题中已经集成了 widgetpack 的星级评分系统，用户无须再安装或引入插件脚本，只需在 widgetpack 中注册账号并修改主题配置即可，应用效果如下：

[![文章评分组件](http://yearito-1256884783.image.myqcloud.com/hexo-advanced-settings/rating.png)](http://yearito-1256884783.image.myqcloud.com/hexo-advanced-settings/rating.png)

[文章评分组件](http://yearito-1256884783.image.myqcloud.com/hexo-advanced-settings/rating.png)



在 [widgetpack](https://widgetpack.com) 中注册账号，根据引导填写应用名称和域名创建应用，创建后可在页面左上角看到应用 id。

在主题配置文件中开启评分功能，填写应用 id，并设置评分颜色：

```
themes\next_config.yml
# Star rating support to each article.
# To get your ID visit https://widgetpack.com
rating:
  enable: true
  id: #<app_id>
  color: fadb14
```

此时刷新浏览器即可在文章末尾看到空的评分栏了。点击评分发现需要以社交账号登陆，而这些社交账号基本都是 facebook、twitter 等墙外的社交软件，限制了评分系统可用性，我们可以在 widgetpack 控制台中修改评分认证机制。

在控制台中点击左上角展开菜单，在 **Rating** -> **Setting** 中将 Vote via 选项改为 Device(cookie) 以开启匿名评分，该选项将基于设备认证访问者身份：

[![开启匿名评分](http://yearito-1256884783.image.myqcloud.com/hexo-advanced-settings/rate-vote-via.png)](http://yearito-1256884783.image.myqcloud.com/hexo-advanced-settings/rate-vote-via.png)

[开启匿名评分](http://yearito-1256884783.image.myqcloud.com/hexo-advanced-settings/rate-vote-via.png)



用户还可以在该页面设定 star 数量和大小。修改后记得勾选右下角的 SAVE SETTING 才会生效。

在实际使用过程中，并非每篇文章都需要开启评分。此时可在 Front-Matter 中设定变量 rating 用于控制是否开启评分。修改文章布局模板中相关代码，使得只有当主题配置文件中 `rating.enable` 字段和 `page.rating` 字段同时为 `true` 才会插入评分组件：

```
themes\next\layout_macro\post.swig
  {% if not is_index %}
-  {% if theme.rating.enable or (theme.vkontakte_api.enable and theme.vkontakte_api.like) or (theme.facebook_sdk.enable and theme.facebook_sdk.like_button) or (theme.needmoreshare2.enable and theme.needmoreshare2.postbottom.enable) or (theme.baidushare and theme.baidushare.type === "button" )%}
+  {% if (theme.rating.enable and post.rating) or (theme.vkontakte_api.enable and theme.vkontakte_api.like) or (theme.facebook_sdk.enable and theme.facebook_sdk.like_button) or (theme.needmoreshare2.enable and theme.needmoreshare2.postbottom.enable) or (theme.baidushare and theme.baidushare.type === "button" )%}
    <div class="post-widgets">
    {% if theme.rating.enable %}
      <div class="wp_rating">
        <div id="wpac-rating"></div>
      </div>
    {% endif %}
```

为了批量为每篇新文章设定该变量并赋默认值，可以修改草稿模板内容，这样以来每篇草稿发布后都会默认开启评分：

```
scaffolds\draft.md
  title: {{ title }}
  tags:
  categories:
+ rating: true
```

站点上线后，可以在控制台菜单的 **Site** -> **Setting** 中勾选 Private，使得组件只对应用内指定的域名上生效，这样以来即时别人错填了你的 id 也不会将评分数据误提交到你的应用中了。

widgetpack 与前文提到的 hotjar 在评价反馈功能上的侧重点不一样，widgetpack 更侧重于对文章的评分，而 hotjar 侧重于对整个页面的评分，并提供了文字和截图反馈的渠道。

## 文章加密访问

该功能由 [hexo-blog-encrypt](https://github.com/MikeCoder/hexo-blog-encrypt) 插件提供，效果如下：

[![文章加密](http://yearito-1256884783.image.myqcloud.com/hexo-advanced-settings/post-encrypt.png)](http://yearito-1256884783.image.myqcloud.com/hexo-advanced-settings/post-encrypt.png)

[文章加密](http://yearito-1256884783.image.myqcloud.com/hexo-advanced-settings/post-encrypt.png)



在站点根目录中执行以下命令安装依赖：

```
$ npm install hexo-blog-encrypt --save
```

在站点配置文件中添加如下字段：

```
_config.yml
encrypt:
  enable: true
  default_abstract: 此文章已被加密，需要输入密码访问。  //首页文章列表中加密文章的默认描述文案
  default_message: 请输入密码以阅读这篇私密文章。  //文章详情页的密码输入框上的默认描述文案
```

然后在文章 Front-Matter 中添加 `password` 字段用于设置文章访问密码。重启服务器，这个时候可能需要经历较长一段时间的加密过程，请耐心等待，加密完成后刷新页面将会显示密码输入框，输入密码后才能继续访问文章内容。

该功能只会加密文章正文，其他内容如打赏、版权信息、标签等则不会被加密隐藏，这样看起来有点奇怪，所以建议加密文章隐藏掉打赏和版权信息内容。

密码输入错误时将会显示浏览器默认告警弹窗，可以使用 [sweetalert](https://sweetalert.js.org/) 来美化错误提示：

在主题自定义布局文件中添加如下代码：

```
themes\next\layout_custom\custom.swig
<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
```

如果 custom.swig 文件不存在，需要手动新建并在布局页面中 body 末尾引入：

```
themes\next\layout_layout.swig
      ...
      {% include '_third-party/exturl.swig' %}
      {% include '_third-party/bookmark.swig' %}
      {% include '_third-party/copy-code.swig' %}

+     {% include '_custom/custom.swig' %}
    </body>
  </html>
```

在 node_modules 依赖库中修改 hexo-blog-encrypt 源码：

```
node_modules\hexo-blog-encrypt\lib\blog.encrypt.js
  ...
  } catch (e) {
-   alert(decryptionError);
+   swal({
+     text: "密码错误!",
+     icon: "error",
+     className: "password-error",
+     timer: 1000,
+     button: false
+   });
    console.log(e);
  }
  ...
```

在自定义样式文件中添加如下代码：

```
themes/next/source/css/_custom/custom.styl
//密码错误sweetalert弹框样式修改
.swal-overlay {
  background-color: transparent;
}

.password-error {
  box-shadow: 0px 4px 12px rgba(0, 0, 0, 0.15);
  border-radius: 4px;
}
```

由于是在 node_module 中修改的依赖文件，一旦更新或者重装依赖都会覆盖修改，需要重新修改一遍。

# 结束语

本文记录了本站在 Next  的基础上的进阶美化方案，除了一些简单的样式修改外，还添加了一些由插件支持的高级动效，包括动态背景、看板娘、边缘摆动、鼠标点击和打字特效等。笔者认为，动效可以使得站点变有趣，但同时也会增加网页的资源消耗，以及影响用户的关注点，有时候会喧宾夺主适得其反，建议珍爱 PC 资源，合理使用动效。

参考链接

- [asdfv1929 | Hexo NexT 主题内给每篇文章后添加结束标语](https://asdfv1929.github.io/2018/01/28/add-the-end/)
- [FJKang | 添加一个萌物](https://fjkang.github.io/2017/12/08/添加一个萌物/)
- [尤雨溪的个人主页](http://evanyou.me/)
- [DIYgod 的博客](https://diygod.me/)
- [猪猪侠的博客](https://www.ofind.cn/)
- [千灵夙赋 | Hexo 优化汇总](https://qianling.pw/hexo-optimization/)
- [龙笑天下 | 给 WordPress 博客网站添加评论输入打字礼花及震动特效](https://www.ilxtx.com/comment-input-effects.html)
